package com.spring.sd;

import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletRegistrationBean;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.mvc.Controller;

/**
 * web容器
 */
public class WebServerApplicationContext {


    public static void main(String[] args) {
        // 1、创建web容器
        AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext(WebConfig.class);

        // 2、运行代码，在浏览器中访问 http://localhost:8080/hello，返回hello
    }


    // 基于 Java 配置类来创建，用于 web 环境。首先定义配置类
    @Configuration
    static class WebConfig {
        @Bean
        public ServletWebServerFactory servletWebServerFactory() {
            // 提供内嵌的 Web 容器
            return new TomcatServletWebServerFactory();
        }

        @Bean
        public DispatcherServlet dispatcherServlet() {
            // 添加前控制器
            return new DispatcherServlet();
        }

        @Bean
        public DispatcherServletRegistrationBean registrationBean(DispatcherServlet dispatcherServlet) {
            // 将 DispatcherServlet 注册到 Tomcat 服务器
            return new DispatcherServletRegistrationBean(dispatcherServlet, "/");
        }

        // 如果 bean 以 '/' 开头，将 '/' 后的 bean 的名称作为访问路径
        @Bean("/hello")
        public Controller controller1() {
            // 添加控制器，用于展示
            return (request, response) -> {
                response.getWriter().println("hello");
                return null;
            };
        }
    }
}
